import Vue from 'vue'
import Vuex from 'vuex'

// 声明使用vuex
Vue.use(Vuex)

export default new Vuex.Store({
  state:{
    order:{
      // 1:{price:12,count:10,name:"coco"},
      // 2:{price:12,count:10,name:"coco"}
    },
    orderAndCount:{
      // 1:10,2:10
    },
    totalPrice:0,
    userInfo: {
      avatarUrl: "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJoclKMxv2TjnFgwEIFdicp2WxZ0S8xMrSBUSHpRj7oY5uQaxgMkuMqBH0nLWNEGrgWZiaJ6Jh7Hzjg/132",
      city: "Wuhan",
      country: "China",
      gender: 1,
      language: "zh_CN",
      nickName: "empiricism",
      province: "Hubei"
    },
    hasUserInfo: false,
    desk:"1",
  },
  mutations:{

    addOrder: function (state, params) {
      // console.log(params);
      if (!state.order[params.id]) {
        state.order[params.id] = {count: 1, name: params.name, price: params.price}
        state.orderAndCount[params.id] = 1;

      } else {
        state.order[params.id]['count']++;
        state.orderAndCount[params.id]++;
      }
      //添加总价
      state.totalPrice += state.order[params.id].price;

      console.log("addOrder:")
      console.log(state.order)
    },
    subOrder(state,id){
      if(state.order[id]['count']>1){
        state.order[id]['count']--
        state.orderAndCount[id]--;
      }else{
        delete state.order[id]
        delete state.orderAndCount[id];
      }

      //添加总价
      state.totalPrice -= state.order[id].price;


      console.log("subOrder:")
      console.log(state.order)
    },
    setOrder(state,params){
      state.order = params;
    },
    setTotalPrice(state,params){
      state.totalPrice = params;
    },
    setOrderAndCount(state,params){
      state.orderAndCount = params;
    },
    setHasUserInfo(state,status){
      console.log("setHasUserInfo : " + status)
      this.hasUserInfo = status;
      console.log("setHasUserInfo : " + this.hasUserInfo)
    },
    setUserInfo(state,params){
      this.state.userInfo = params
    },
    setDesk(state,id){
      state.desk = id;
    }
  },
  getters:{
    getDesk(state){
      return state.desk;
    },
    getOrder(state){
      return state.order;
    },
    getOrderCountById(state,getters){
      return function (id) {
        if (state.order[id]) {
          return state.order[id].count;
        } else {
          return 0;
        }
      }
    },
    test(state,getters){
      return function (id) {
        return id*2;
      }
    },
    //获得总价
    getTotalPrice(state){
        return state.totalPrice*100;
    },
    getOrderAndCount(state){
      return state.orderAndCount;
    },
    getHasUserInfo(state){
      return state.hasUserInfo;
    },
    getStoreUserInfo(state){
      return state.userInfo;
    }
  }

})
